package com.lovo.Atm;

import com.lovo.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Transfer extends JPanel implements ActionListener {
    private Home frame;
    private JButton backImg;
    private JButton sureImg;
    private JLabel noteAccount;
    private JLabel noteBlance;
    private JTextField printAcc;
    private JTextField printBla;
    private Icon backBtn = Util.iconImg("/images/buttonOff.jpg");
    private Icon sureBtn = Util.iconImg("/images/buttonSure.jpg");

    public Transfer(Home frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        //用户名提示和输入框
        noteAccount = new JLabel("请输入转账用户名：");
        noteAccount.setFont(new Font("宋体", Font.BOLD, 20));
        noteAccount.setBounds(350, 200, 250, 30);
        this.add(noteAccount);

        printAcc = new JTextField();
        printAcc.setBounds(620, 200, 100, 30);
        this.add(printAcc);

        //转账金额
        noteBlance = new JLabel("请输入转账金额：");
        noteBlance.setFont(new Font("宋体", Font.BOLD, 20));
        noteBlance.setBounds(350, 250, 250, 30);
        this.add(noteBlance);

        printBla = new JTextField();
        printBla.setBounds(620, 250, 100, 30);
        this.add(printBla);

        //确认按钮
        sureImg = new JButton(sureBtn);
        sureImg.setBounds(0, 400, 140, 50);
        this.add(sureImg);
        sureImg.addActionListener(this);

        //退出按钮
        backImg = new JButton(backBtn);
        backImg.setBounds(870, 400, 140, 50);
        this.add(backImg);
        backImg.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (backImg == e.getSource()) {
            frame.switchPanel("menu");
        }
        if (sureImg == e.getSource()) {
            boolean flag = false;
            //得到转账用户名，判断用户是否存在于系统中，不能给自己转账\
            String text = printAcc.getText();
            //得到转账金额，以及用户余额判断是否有足够的余额转账
            double a = frame.getLoginUser().getBlance();//本金
            double b = Double.valueOf(printBla.getText());//转账金额
            for (int i = 0; i < frame.getUser().size(); i++) {
                if (text.equals(frame.getUser().get(i).getName()) && !text.equals(frame.getLoginUser().getName())) {
                    flag = true;
                    if (b > a) {
                        JOptionPane.showMessageDialog(this, "账户余额不足，请重新输入转账金额！");
                        printBla.setText("");
                    } else if (a >= b) {
                        //修改本金
                        frame.getLoginUser().setBlance(a - b);
                        frame.getFindPanel().set(a - b);
                        //修改转账用户余额
                        frame.getUser().get(i).setBlance(frame.getUser().get(i).getBlance() + b);
                        JOptionPane.showMessageDialog(this, "转账金额为：" + b + "。转账成功！");
                        printBla.setText("");
                        printAcc.setText("");
                        frame.switchPanel("menu");
                    }
                }
            }
            if (!flag) {
                printBla.setText("");
                printAcc.setText("");
                JOptionPane.showMessageDialog(this, "账户输入有误！");
                frame.switchPanel("menu");
            }

        }
    }
}
